#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iniparser.h>

#include "mqtt.h"

#define INI_PATH "mqtt.ini"


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iniparser.h>

#include "mqtt.h"

#define INI_PATH "mqtt.ini"

int mqtt_configuration(void)
{
    dictionary *ini = NULL;
    const char *str;

    // Initialize `conn_para` with zeroes
//    memset(&conn_para, 0, sizeof(conn_para_t));
    // Load ini file, store data in ini structure
    ini = iniparser_load(INI_PATH);
    if (ini == NULL) {
        return -1;
    }

    str = iniparser_getstring(ini, "mqtt:hostname", "NULL");
    if (str != NULL) {
        snprintf(conn_para.hostname, sizeof(conn_para.hostname), "%s", str);
    }

    str = iniparser_getstring(ini, "mqtt:pub_topic", "NULL");
    if (str != NULL) {
        snprintf(conn_para.pub_topic, sizeof(conn_para.pub_topic), "%s", str);
    }

    str = iniparser_getstring(ini, "mqtt:clientId", "NULL");
    if (str != NULL) {
        snprintf(conn_para.clientId, sizeof(conn_para.clientId), "%s", str);
    }

    str = iniparser_getstring(ini, "mqtt:username", "NULL");
    if (str != NULL) {
        snprintf(conn_para.username, sizeof(conn_para.username), "%s", str);
    }

    str = iniparser_getstring(ini, "mqtt:password", "NULL");
    if (str != NULL) {
        snprintf(conn_para.password, sizeof(conn_para.password), "%s", str);
    }

    str = iniparser_getstring(ini, "mqtt:port", "NULL");
    if (str != NULL) {
        conn_para.port = atoi(str);
    }

    str = iniparser_getstring(ini, "mqtt:sub_topic", "NULL");
    if (str != NULL) {
        snprintf(conn_para.sub_topic, sizeof(conn_para.sub_topic), "%s", str);
    }

    str = iniparser_getstring(ini, "mqtt:keepalive", "NULL");
    if (str != NULL) {
        conn_para.keepalive = atoi(str);
    }

    str = iniparser_getstring(ini, "mqtt:qos", "NULL");
    if (str != NULL) {
        conn_para.qos = atoi(str);
    }

    iniparser_freedict(ini);
    return 0;
}

